package io.forlove.mytool.mytool.ccdownload;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.commons.codec.binary.BaseNCodec;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

@Service
public class BilibiliCCDonwload {

    @Autowired
    private RestTemplate restTemplate;


    public void getPage(String url) throws IOException {

        String ret = restTemplate.getForObject(url, String.class);

        String patternString = "subtitle_url(.+?)json";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(ret);
        String content="";
        while(matcher.find()) {
           content =StringEscapeUtils.unescapeJava(matcher.group().substring("subtitle_url\":\"".length()));
        }

        ret=restTemplate.getForObject(content,String.class);

        JsonMapper jsonMapper =new JsonMapper();
        JsonNode jm = jsonMapper.readTree(ret);
        JsonNode body = jm.get("body");
        for(int i=0;i<body.size();i++){

            String text = body.get(i).get("content").asText();
            System.out.println(text);
        }
    }

    public void getPages(String bvid) throws IOException {

        String ret = restTemplate.getForObject("https://api.bilibili.com/x/player/pagelist?bvid=" + bvid, String.class);

        JsonMapper jsonMapper = new JsonMapper();
        JsonNode jm = jsonMapper.readTree(ret);

        JsonNode datas = jm.get("data");

        List<String> cids = new ArrayList<>();
        for (int i=0;i< datas.size();i++){
//        for (int i=0;i< 10;i++){
           cids.add(String.valueOf(datas.get(i).get("cid").intValue()));
        }

        int id=1;
        for (String cid : cids) {

            ret = restTemplate.getForObject("https://api.bilibili.com/x/player/v2?cid=" + cid + "&bvid=" + bvid, String.class);
            jsonMapper = new JsonMapper();
            jm = jsonMapper.readTree(ret);
            JsonNode st = jm.get("data").get("subtitle").get("subtitles");
            StringBuilder contents = new StringBuilder();
            StringBuilder contentsCh = new StringBuilder();
            for (int i = 0; i < st.size(); i++) {

                if("en-US".equals(st.get(i).get("lan").textValue())) {

                    String content = "http:" + st.get(i).get("subtitle_url").asText();
                    ret = restTemplate.getForObject(content, String.class);
                    jsonMapper = new JsonMapper();
                    jm = jsonMapper.readTree(ret);
                    JsonNode body = jm.get("body");
                    for (int j = 0; j < body.size(); j++) {
                        String text = body.get(j).get("content").asText();
                        contents.append(text).append(System.lineSeparator()).append(System.lineSeparator());
                    }
                }else{

                    String content = "http:" + st.get(i).get("subtitle_url").asText();
                    ret = restTemplate.getForObject(content, String.class);
                    jsonMapper = new JsonMapper();
                    jm = jsonMapper.readTree(ret);
                    JsonNode body = jm.get("body");
                    for (int j = 0; j < body.size(); j++) {
                        String text = body.get(j).get("content").asText();
                        contentsCh.append(text).append(System.lineSeparator());
                    }

                }
            }
            Files.writeString(Path.of("D:\\zimu",(id++)+".md"),contents.toString()+contentsCh);
            System.out.println("id:"+id+" finished");
        }


    }
}
